package android.test;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.provider.CallLog;
import android.view.Menu;
import android.view.View;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class CallLogActivity extends Activity {

    public static final String CALL_TYPE = "type";
    public static final int OUT_GOING_CALL  = 1;
    public static final int INCOMING_CALL  = 2;
    public static final int MISS_CALL  = 3;
    public static ArrayList<CallLogModel> outgoingcalllist;
    public static ArrayList<CallLogModel> incomingcalllist;
    public static ArrayList<CallLogModel> misscalllist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calllogmain);

        outgoingcalllist = new ArrayList<CallLogModel>();
        incomingcalllist = new ArrayList<CallLogModel>();
        misscalllist = new ArrayList<CallLogModel>();

        new ReadLogs().execute();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.call_log, menu);
        return true;
    }

    public  void outGoingCallClick(View v){
        Intent i = new Intent(CallLogActivity.this, CallLogListActivity.class);
        i.putExtra(CALL_TYPE, OUT_GOING_CALL);
        startActivity(i);
    }
    public void incomingCallClick(View v){
        Intent i = new Intent(CallLogActivity.this, CallLogListActivity.class);
        i.putExtra(CALL_TYPE, INCOMING_CALL);
        startActivity(i);

    }
    public void missCallClick(View v){
        Intent i = new Intent(CallLogActivity.this, CallLogListActivity.class);
        i.putExtra(CALL_TYPE, MISS_CALL);
        startActivity(i);

    }
    public void readCallLog(){
        misscalllist.clear();
        outgoingcalllist.clear();
        incomingcalllist.clear();

        Cursor callLogCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
        if(callLogCursor != null){

            while(callLogCursor.moveToNext()){
                String id = callLogCursor.getString(callLogCursor.getColumnIndex(CallLog.Calls._ID));
                String name = callLogCursor.getString(callLogCursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
                String cacheNumber = callLogCursor.getString(callLogCursor.getColumnIndex(CallLog.Calls.CACHED_NUMBER_LABEL));
                String number = callLogCursor.getString(callLogCursor.getColumnIndex(CallLog.Calls.NUMBER));
                long dateTimeMillis = callLogCursor.getLong(callLogCursor.getColumnIndex(CallLog.Calls.DATE));
                long durationMillis = callLogCursor.getLong(callLogCursor.getColumnIndex(CallLog.Calls.DURATION));
                int callType = callLogCursor.getInt(callLogCursor.getColumnIndex(CallLog.Calls.TYPE));

                String duration = getDuration(durationMillis);
                String dateTime = getDateTime(dateTimeMillis);

                if(cacheNumber == null){
                    cacheNumber = number;
                }
                if(name == null){
                    name = "No Name";
                }

                CallLogModel callLogModel = new CallLogModel(name, cacheNumber, dateTime, duration);
                if(callType == CallLog.Calls.OUTGOING_TYPE){
                    outgoingcalllist.add(callLogModel);
                }else if(callType == CallLog.Calls.INCOMING_TYPE){
                    incomingcalllist.add(callLogModel);
                }else if(callType == CallLog.Calls.MISSED_TYPE){
                    misscalllist.add(callLogModel);
                }

            }

            callLogCursor.close();

        }

    }

    public String getDuration(long durationMillis){
        String s;
        int minute = (int)durationMillis/60;
        int second = (int)durationMillis % 60;
        s = minute + "m " + second + "s";
        return s;
    }

    public String getDateTime(long dateTimeMillis){
        String s;
        s = new SimpleDateFormat("dd/MM/yyyy-HH:mm:ss").format(dateTimeMillis).toString();
        return  s;

    }

    private class ReadLogs extends AsyncTask<Void, Void, Void>{

        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = ProgressDialog.show(CallLogActivity.this, "Reading Callogs...", "Please wait!", true);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            dialog.dismiss();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            readCallLog();
            return null;
        }
    }
    
}
